package com.example.demo.config;

import com.example.demo.config.recode.ReCodeAuthenticationSecurityConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Configuration
public class LearnSecurityConfig {


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @EnableWebSecurity
    @Component
    @Configuration
    @EnableGlobalMethodSecurity(prePostEnabled = true)
    public static class WxSecurityConfig extends WebSecurityConfigurerAdapter {
        @Autowired
        ReCodeAuthenticationSecurityConfig reCodeAuthenticationSecurityConfig;

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.apply(reCodeAuthenticationSecurityConfig)
                    .and()
                    .authorizeRequests ()
                    .antMatchers ("/login/**","/login.html","/static/**" ,"/**/favicon.ico","/favicon.ico").permitAll ()

                    .and()
                    .formLogin().loginPage( "/login.html" )
                    .and()
                    .antMatcher("/**").authorizeRequests()
                    .anyRequest().authenticated().and ().csrf ().disable ().headers ().frameOptions ().sameOrigin ();
            ;
        }
    }
}
